// packageC/pages/ssxl/ssxl.js
const app = getApp();
const sendMessage = require('../../../pages/home/home')
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:["/images/up.png","/images/right.png","/images/bottom.png","/images/left.png"],
    second:180,
    times:0,
    trainType:'',
    sbList:[],
    contentList:[],
    page:0,
    inter:'',
    equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var trainType = options.trainType;
    var time = options.time;
    if (trainType == '视标扫视'){
      var list = [];
      for(var i=0;i<24;i++){
        let random = Math.floor(Math.random() * 4);
        var selected = false;
        if(i == 0){
          selected = true;
        }
        list.push({"random":random,"num":i});
      }
      that.setData({
        trainType:trainType,
        sbList:list,
        second:time * 60
      });
    } else if (trainType == '文章阅读'){
      that.setData({
        trainType:trainType,
        second:time * 60
      });
      util.saoshiList(that);
    }
    this.startInterval();
    this.startCountdown();
  },
  upPage(){
    var that = this;
    var list = [];
    for(var i=0;i<24;i++){
      let random = Math.floor(Math.random() * 4);
      var selected = false;
      if(i == 0){
        selected = true;
      }
      list.push({"random":random,"num":i});
    }
    that.setData({
      sbList:list
    });
  },
  nextPage(){
    var that = this;
    var list = [];
    for(var i=0;i<24;i++){
      let random = Math.floor(Math.random() * 4);
      var selected = false;
      if(i == 0){
        selected = true;
      }
      list.push({"random":random,"num":i});
    }
    that.setData({
      sbList:list
    });
  },
  /**上一页 */
upPages(e){
  var that = this;
  var page = e.target.dataset.page;
  if (page == 0){
    wx.showToast({
      title: '已经是第一页了',
      icon: 'none',
      duration: 2000
    })
    return;
  } else{
    page = page -1;
    that.setData({
      page:page
    })
    util.saoshiList(that)
  }
},
/**下一页 */
nextPages(e){
  var that = this;
  var page = e.target.dataset.page;
  page = page +1;
  that.setData({
    page:page
  })
  util.saoshiList(that)
},
startCountdown() {
  var that = this;
  var second = that.data.second;
  var equipmentNumber = app.nowEquipmentId;
  if (second == 0) {
    clearInterval(that.data.timer);
    clearInterval(that.data.inter);
    var score = 5;
    wx.navigateTo({
      url: '../../../pages/fraction/fraction?fraction='+score,
    })
  }
  that.data.timer = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    that.startCountdown();
  } , 1000)
  var times = that.data.times;
  times++;
  that.setData({
    times:times,
    equipmentNumber:equipmentNumber
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"扫视训练",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"扫视训练",
      openId:openId
    })
    util.saveRecord(this);
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})